package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.vo.WxCatelogIndexVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gatsby
 * @since 2022/10/14 18:57
 */
@Service
public class WxCatelogServiceImpl implements WxCatelogService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
     * 查询全部商品类目的业务逻辑
     *
     * @return com.cskaoyan.bean.vo.WxCatelogIndexVo
     * @author gatsby
     * @since 2022/10/14 19:08
     */
    @Override
    @Transactional
    public WxCatelogIndexVo queryAllCatelog() {
        WxCatelogIndexVo wxCatelogIndexVo = new WxCatelogIndexVo();

        //先查询全部pid为0的类目
        MarketCategoryExample example = new MarketCategoryExample();
        example.setDistinct(true);
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(example);
        //将该结果加入到返回的Vo中
        wxCatelogIndexVo.setCategoryList(categoryList);
        //再查询出当前的类目，即id最小的
        MarketCategory currentCategory = categoryList.get(0);
        //把这个结果塞到vo
        wxCatelogIndexVo.setCurrentCategory(currentCategory);

        //再查询出当前类目的所有子类目
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.setDistinct(true);
        MarketCategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andPidEqualTo(currentCategory.getId());
        criteria1.andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = marketCategoryMapper.selectByExample(categoryExample);
        //把这个结果也塞进vo
        wxCatelogIndexVo.setCurrentSubCategory(currentSubCategory);
        return wxCatelogIndexVo;
    }

    /**
     * 查询当前的类目信息
     *
     * @param id
     * @return com.cskaoyan.bean.vo.WxCatelogIndexVo
     * @author gatsby
     * @since 2022/10/14 19:37
     */
    @Override
    public WxCatelogIndexVo queryCurrentCategory(Integer id) {
        WxCatelogIndexVo wxCatelogIndexVo = new WxCatelogIndexVo();

        //根据id获取当前类目信息
        MarketCategoryExample example = new MarketCategoryExample();
        example.setDistinct(true);
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> currentCategorys = marketCategoryMapper.selectByExample(example);
        MarketCategory currentCategory = currentCategorys.get(0);
        //将这个结果塞进Vo
        wxCatelogIndexVo.setCurrentCategory(currentCategory);

        //查找当前类目的子目录
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.setDistinct(true);
        MarketCategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andPidEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = marketCategoryMapper.selectByExample(categoryExample);
        //将这个结果塞到vo里面去
        wxCatelogIndexVo.setCurrentSubCategory(currentSubCategory);

        return wxCatelogIndexVo;
    }
}
